﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;



namespace Terrain44
{
    class Shockwave
    {
        private SpriteBatch spriteBatch;

      
        
        public Texture2D shockwave;

        private Vector3 screenStartPosition; 

        RenderTarget2D finalImage;
        RenderTarget2D distortionMap;

        Effect distortionEffect;

        public int currentSize
        {
            get;
            private set;
        }

        int maxSize;

        GraphicsDevice device;
        GraphicsDeviceManager graphics;

        Matrix worldMatrix;
        Matrix viewMatrix;
        Matrix projectionMatrix;


        


        public Shockwave(Texture2D shockwave, Vector3 worldStartPosition, Effect distortionEffect, int maxSize, GraphicsDevice device,  GraphicsDeviceManager graphics, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix)
        {
           
            this.shockwave = shockwave;
            this.distortionEffect = distortionEffect;
            this.maxSize = maxSize;
            this.device = device;
            this.graphics = graphics;
            this.worldMatrix = worldMatrix;
            this.viewMatrix = viewMatrix;
            this.projectionMatrix = projectionMatrix;
            this.screenStartPosition = device.Viewport.Project(worldStartPosition, projectionMatrix, viewMatrix, worldMatrix);

            spriteBatch = new SpriteBatch(device);

            finalImage = new RenderTarget2D(device, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);

            PresentationParameters pp = device.PresentationParameters;
            distortionMap = new RenderTarget2D(device, pp.BackBufferWidth, pp.BackBufferHeight, true, device.DisplayMode.Format, DepthFormat.Depth24);
            //distortionMap = new RenderTarget2D(device, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);

            currentSize = 0;
        }

        public bool HasFinished()
        {
            if (currentSize >= maxSize)
            {
                return true;
            }
            return false;
        }

        public void Update()
        {
            if (!HasFinished())
            {
                currentSize += 30;
            }
        }

        public void DrawShockwave(RenderTarget2D backgroundScene)
        {
            if (!HasFinished())
            {
                
                #region Szene zeichnen

                ////Ab jetzt wird auf finalImage statt direkt gezeichnet
                //GraphicsDevice.SetRenderTarget(finalImage);
                ////finalImage mit Blau ausfüllen
                //GraphicsDevice.Clear(Color.CornflowerBlue);

                //spriteBatch.Begin();
                ////Hintergrund auf finalImage zeichnen
                //spriteBatch.Draw(back, Vector2.Zero, Color.White);
                //spriteBatch.End();

                #endregion

                #region Druckwellen zeichnen

                //Druckwelle zeichnen
                //Ab jetzt wird auf distortionMap statt direkt gezeichnet
                device.SetRenderTarget(distortionMap);

                
                
                

                //distortionMap mit Schwarz (keine verzerrung) ausfüllen
                device.Clear(Color.Black);

                

                spriteBatch.Begin();

                
                spriteBatch.Draw(shockwave, new Rectangle((int)(screenStartPosition.X - (currentSize / 2)), (int)(screenStartPosition.Y - (currentSize / 2)),
                        (int)currentSize, (int)currentSize), Color.White);
                spriteBatch.End();

                //Ab jetzt wieder direkt zeichnen lassen
                device.SetRenderTarget(null);
                
                #endregion


                //Scene mit distortionMap zeichnen

               
                #region finalScene zeichnen
                device.Textures[0] = backgroundScene;
                device.Textures[1] = distortionMap;

                //spriteBatch mit dem Shader aufrufen
                spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, distortionEffect);


                device.BlendState = BlendState.Opaque;
                device.DepthStencilState = DepthStencilState.Default;
                
                
                //finalImage zeichnen
                spriteBatch.Draw(backgroundScene, Vector2.Zero, Color.White);
                spriteBatch.End();
                backgroundScene.Dispose();
                #endregion
            }
            }

           

            
        }

    
}
